/*
 * > /*
 >  * INSANE - Interactive Structural Analysis Environment
 >  *
 >  * Copyright (C) 2003-2004
 >  * Universidade Federal de Minas Gerais
 >  * Escola de Engenharia
 >  * Departamento de Engenharia de Estruturas
 >  *
 >  * Author's email :     insane@dees.ufmg.br
 >  * Author's Website :   http://www.dees.ufmg.br/insane
 >  *
 >  * This program is free software; you can redistribute it and/or
 >  * modify it under the terms of the GNU General Public License
 >  * as published by the Free Software Foundation; either version 2
 >  * of the License, or any later version.
 >  *
 >  * This program is distributed in the hope that it will be useful,
 >  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 >  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 >  * GNU General Public License for more details.
 >  *
 >  * You should have received a copy of the GNU General Public License
 >  * along with this program; if not, write to the Free Software
 >  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
 > 1307, USA.
 *
 */

package br.ufmg.dees.insane.ui.rich.learn.learn_controller;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.ListIterator;

import br.ufmg.dees.insane.draw.Draw;
import br.ufmg.dees.insane.draw.EquationsDraw;
import br.ufmg.dees.insane.model.disc.driver.StructuralMech;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.model.disc.node.Node;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.IView;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.equation_view.EquationsView;
import br.ufmg.dees.insane.util.IPoint3d;

/**
 * 
 * @author Moreira, Renata & Pitangueira, Roque
 * @since May 2005
 */
public class EquationsController extends LearnController {
	
	protected ArrayList<Draw> equations = new ArrayList<Draw>();
    
	public EquationsController(FemModel model) {
        this.model = model;
    }

    
    public EquationsController(FemModel model, IView eview) {
        this.model = model;
        this.view = eview;
    }

    public EquationsController(FemModel model, IView eview, StructuralMech dr) {
        this.model = model;
        this.view = eview;
        this.driver = dr;
    }
    
    public EquationsController(IView eview) {
        this.view = eview;
    }

    public EquationsController() {
    }
    
    public void setModel(FemModel newModel) {
        this.model = newModel;
    }

    public void setView(EquationsView newView) {
        this.view = newView;
    }
    
    public void draw(Graphics2D g) {
        super.draw(g);
        Draw draw;
        Node no;
        // Draws equations
        g.setColor(this.view.getViewState().equationsColor);
        ListIterator ea = equations.listIterator();
        while (ea.hasNext()) {
            draw = (Draw) ea.next();
            no = (Node) draw.getRepresents();
            IPoint3d p3 = no.getCoord();
            draw.setLocation(this.view.getDrawingArea().getViewPoint(
                    new Point2D.Double(p3.x, p3.y)));
            draw.draw(g);
        }
    }
    
    public void select(Point p) {
		Draw draw;
		int index;
		ListIterator pts = nodes.listIterator();
        while (pts.hasNext()) {
            draw = (Draw) pts.next();
            if (draw.contains(p)) {
                if (draw.isSelected()) {
                    draw.setSelected(false);
                    index = selection.indexOf(draw.getRepresents());
                    selection.remove(index);
                    return;
                } else {
                    if (draw.isSelectable()) {
                        draw.setSelected(true);
                        selection.add(draw.getRepresents());
                    }
                    return;
                }
            }
        }
	}
    
    public void selectAll() {
        Draw draw;
		selection.clear();
		ListIterator pts = nodes.listIterator();
        while (pts.hasNext()) {
            draw = (Draw) pts.next();
            draw.setSelected(true);
            selection.add(draw.getRepresents());
        }
	}
    
    public void unSelectAll() {
        Draw draw;
		selection.clear();
		ListIterator pts = nodes.listIterator();
        while (pts.hasNext()) {
            draw = (Draw) pts.next();
            draw.setSelected(false);
        }
	}
    
    public boolean isSelected() {
        Draw draw;
        ListIterator pts = nodes.listIterator();
        while (pts.hasNext()) {
            draw = (Draw) pts.next();
            if (draw.isSelected()) {
                return true;
            }
        }
        return false;
  }
    
    public void compose() {
    	//((LearnController)this.getView().getController()).getDriver().zeroNodeEquations();
        this.removeAll();
        super.compose();
        ListIterator femnodes = model.getNodesList().listIterator();
        Node node;
        while (femnodes.hasNext()) {
            node = (Node) femnodes.next();
            //Fx equations
            if (node.getEquation(0) > 0) {
                Draw draw = new EquationsDraw.Out();
                draw.setRepresents(node);
                ((EquationsDraw) draw).setValue(10);
                draw.setAngle(node.getAngle() * Math.PI / 180);
                draw.setLabel(String.valueOf(node.getEquation(0)));
                equations.add(draw);
            }
            //Fy equations
            if (node.getEquation(1) > 0) {
                Draw draw = new EquationsDraw.Out();
                draw.setRepresents(node);
                ((EquationsDraw) draw).setValue(10);
                draw.setAngle(Math.toRadians(90) + node.getAngle() * Math.PI
                        / 180);
                draw.setLabel(String.valueOf(node.getEquation(1)));
                equations.add(draw);
            }
            //Mz equations
            if (node.getEquation(5) > 0) {
                Draw draw = new EquationsDraw.AntiClockwise();
                draw.setRepresents(node);
                ((EquationsDraw) draw).setValue(10);
                draw.setLabel(String.valueOf(node.getEquation(5)));
                equations.add(draw);
            }
            //End of adding equations
        }

    }
    public void removeAll() {
        equations.clear();
    }

  public ArrayList<Draw> getEquations() {
    return equations;
  }
}
